package Messages;

import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import Server.SQLConnection;
import ocsf.server.ConnectionToClient;

public class MessageSaveStep {
	
	private ConnectionToClient client ;
	private ArrayList<Object> str ; 
	public static String TableName;
	static int row1;
	private ResultSet RunNewRs;
	
public MessageSaveStep(ArrayList<Object> msg, ConnectionToClient client) {
		
		this.client= client ;
		this.str= msg; 	
		
	}



public void Run(String ScenarioName, String Expected, String Actual, String TestStep, String Comment, String Description) throws SQLException, IOException {
	
	SQLConnection sc = new SQLConnection();
	PreparedStatement Scenariops = sc.getConnection().prepareStatement("INSERT INTO hts."+ScenarioName+" (idScenario,TestDescription,Teststep,Expected,Actual,Status,Comment) VALUES(?,?,?,?,?,?,?);");
	PreparedStatement AllScenario = sc.getConnection().prepareStatement("SELECT * FROM hts."+ScenarioName+";");
	
	ResultSet Allrs= AllScenario.executeQuery();
	row1 = 0;
	while(Allrs.next()){
		int temp = Allrs.getInt(1);
			if(row1>temp)
				row1=row1;
				else{
				row1=temp;
				}
	
	}
	Allrs.close();

	Scenariops.setInt(1, this.row1+1);
	Scenariops.setString(2,Description);
	Scenariops.setString(3,TestStep);
	Scenariops.setString(4,Expected);
	Scenariops.setString(5,Actual);
	Scenariops.setString(6,"NoRun");
	Scenariops.setString(7,Comment);
	Scenariops.executeUpdate();
	Scenariops.close();
	
	java.sql.PreparedStatement ScenarioTemp = sc.getConnection().prepareStatement("SELECT t.`idScenario`,t.`TestDescription`,t.`Status` FROM hts."+ScenarioName+" t;");


	RunNewRs= ScenarioTemp.executeQuery();
	

	ArrayList<Object> Arr=new ArrayList<Object>();
    Arr.add("AddScenario");
    Arr.add(ScenarioName);
    
	while (RunNewRs.next())
	{
		Arr.add(RunNewRs.getInt(1));
		Arr.add(RunNewRs.getString(2));
		
		Arr.add(RunNewRs.getString(3));
		

	}
	
   
    
	client.sendToClient(Arr);
	
	

	
}

}//End of class
